package com.framework.admin.controller;

import com.framework.admin.repository.SysRoleRepository;
import com.framework.admin.domain.vo.DeptUserTreeVo;
import com.framework.admin.domain.vo.RoleMenu;
import com.framework.admin.model.SysRole;
import com.framework.admin.model.SysUser;
import com.framework.admin.service.IRoleService;
import com.framework.common.domain.ParamPage;
import com.framework.common.domain.ResultData;
import com.framework.common.util.BeanUtils;
import com.framework.common.vo.TreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Api(value = "role", tags = "角色管理")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private SysRoleRepository sysRoleRepository;
    /**
     * 获取角色列表
     */
    @ResponseBody
    @ApiOperation(value = "获取角色列表")
    @PreAuthorize("hasAuthority('100')")
    @GetMapping(value = "/list")
    public ResultData<Page<SysRole>> list(SysRole role, ParamPage page) {
        PageRequest request = PageRequest.of(page.getPageNo(), page.getPageSize());
        Page<SysRole> tree = roleService.getRoleList(role, request);
        return ResultData.data(tree);
    }

    @ResponseBody
    @ApiOperation(value = "获取角色列表")
    @PostMapping(value = "/all")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<List<SysRole>> allList(SysRole role) {
        List<SysRole> list = roleService.allList(role);
        return ResultData.data(list);
    }

    /**
     * 获取角色
     */
    @ResponseBody
    @ApiOperation(value = "获取角色")
    @GetMapping(value = "/user/{id}")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<List<String>> getRoleByUserId(@PathVariable String id) {
        List<String> ids = roleService.getRoleByUserId(id);
        return ResultData.data(ids);
    }

    /**
     * 获取角色列表, 前端角色列表不分页，全部加载
     */
    @ResponseBody
    @ApiOperation(value = "获取所有角色列表")
    @GetMapping(value = "/tree")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> list(SysRole role) {
        List<TreeNode<SysRole>> tree = roleService.getTree(role);
        return ResultData.data(tree);
    }

    /**
     * 获取getOne
     */
    @ApiOperation(value = "获取getOne")
    @GetMapping(value = "/getOne")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> getOne(@RequestParam String id) {
        SysRole sysRole = sysRoleRepository.findOne(id);
        return ResultData.data(sysRole);
    }
    /**
     * 保存角色
     */
    @ResponseBody
    @ApiOperation(value = "保存角色")
    @PostMapping(value = "/save")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> save(@RequestBody SysRole role) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(role, sysRole);

        if (StringUtils.isNotEmpty(role.getId())) {
            roleService.update(role);
        } else {
            roleService.save(role);
        }

        return ResultData.success();
    }

    /**
     * 删除角色
     */
    @ResponseBody
    @ApiOperation(value = "删除角色")
    @PostMapping(value = "/del")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> del(@RequestBody List<String> ids) {
        roleService.delete(ids);
        return ResultData.success();
    }

    /**
     * 修改角色菜单
     */
    @ResponseBody
    @ApiOperation(value = "修改角色菜单")
    @PostMapping(value = "/menu")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> updateRoleMenu(@RequestBody RoleMenu roleMenu) {
        roleService.updateRoleMenu(roleMenu.getId(), roleMenu.getMenus());
        return ResultData.success();
    }

    /**
     * 获取角色列表
     */
    @ResponseBody
    @ApiOperation(value = "获取角色菜单列表")
    @GetMapping(value = "/menu")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> getMenuCodes(SysRole role) {
        List<String> codes = roleService.getMenuCodes(role);
        return ResultData.data(codes);
    }

    @ApiOperation(value = "获取角色用户信息")
    @GetMapping(value = "/getRoleUserInfo")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> getRoleUserInfo(@RequestParam String roleId) {
        List<SysUser> list = roleService.getRoleUserInfo(roleId);
        return ResultData.data(list);
    }

    @ApiOperation(value = "获取部门用户树信息")
    @GetMapping(value = "/getDeptUserTree")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> getDeptUserTree() {
        List<DeptUserTreeVo> deptUserTree = roleService.getDeptUserTree();
        return ResultData.data(deptUserTree);
    }

    @ApiOperation(value = "绑定用户角色")
    @PostMapping(value = "/bindUserRole")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> bindUserRole(@RequestParam String roleId, @RequestParam String userIds) {
        List<String> userIdsList= Arrays.asList(userIds.split(","));
        roleService.bindUserRole(roleId, userIdsList);
        return ResultData.success();
    }

    @ApiOperation(value = "取消绑定用户角色")
    @PostMapping(value = "/unbindUserRole")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> unbindUserRole(@RequestParam String roleId, @RequestParam String userIds) {
        List<String> userIdsList= Arrays.asList(userIds.split(","));
        roleService.unbindUserRole(roleId, userIdsList);
        return ResultData.success();
    }

    @ApiOperation(value = "根据树的回显信息id-name")
    @GetMapping(value = "/getTreeInfoByRoleId")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> getTreeInfoByRoleId(@RequestParam String roleId) {
        List<Map<String,String>> userIds = roleService.getTreeInfoByRoleId(roleId);
        return ResultData.data(userIds);
    }
}
